package com.chushouya.order.rabbitmq.sender;

import com.chushouya.common.constant.RabbitMqQueueEnum;
import com.chushouya.common.util.RabbitMQUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * 铛铛旧衣订单消息发送器
 * 用于发送铛铛旧衣订单处理消息
 * 
 * @author chushouya
 */
@Component
@Slf4j
public class DangDangOrderMessageSender {

    @Resource
    private RabbitMQUtil rabbitMQUtil;

    /**
     * 发送铛铛订单处理消息（立即发送）
     * 
     * @param orderId 订单ID
     */
    public void sendDangDangOrderMessage(Long orderId) {
        try {
            if (orderId == null || orderId <= 0) {
                log.error("订单ID无效，无法发送铛铛订单消息: orderId={}", orderId);
                return;
            }

            log.info("发送铛铛订单处理消息，订单ID: {}", orderId);
            rabbitMQUtil.sendMessage(RabbitMqQueueEnum.DANGDANG_ORDER.getQueueName(), orderId);
            log.info("铛铛订单处理消息发送成功，订单ID: {}", orderId);
        } catch (Exception e) {
            log.error("发送铛铛订单处理消息异常，订单ID: {}, 错误信息: {}", orderId, e.getMessage(), e);
        }
    }

    /**
     * 发送铛铛订单处理消息（延时发送 - 秒）
     * 
     * @param orderId 订单ID
     * @param delaySeconds 延时秒数
     */
    public void sendDangDangOrderMessage(Long orderId, int delaySeconds) {
        try {
            if (orderId == null || orderId <= 0) {
                log.error("订单ID无效，无法发送铛铛订单延时消息: orderId={}", orderId);
                return;
            }

            log.info("发送铛铛订单处理延时消息，订单ID: {}, 延时: {}秒", orderId, delaySeconds);
            rabbitMQUtil.sendDelayMessage(
                RabbitMqQueueEnum.DANGDANG_ORDER.getQueueName(), 
                orderId, 
                delaySeconds, 
                TimeUnit.SECONDS
            );
            log.info("铛铛订单处理延时消息发送成功，订单ID: {}, 延时: {}秒", orderId, delaySeconds);
        } catch (Exception e) {
            log.error("发送铛铛订单处理延时消息异常，订单ID: {}, 延时: {}秒, 错误信息: {}", 
                    orderId, delaySeconds, e.getMessage(), e);
        }
    }

    /**
     * 发送铛铛订单处理消息（延时分钟）
     * 
     * @param orderId 订单ID
     * @param delayMinutes 延时分钟数
     */
    public void sendDangDangOrderMessageDelayMinutes(Long orderId, int delayMinutes) {
        try {
            if (orderId == null || orderId <= 0) {
                log.error("订单ID无效，无法发送铛铛订单延时消息: orderId={}", orderId);
                return;
            }

            log.info("发送铛铛订单处理延时消息，订单ID: {}, 延时: {}分钟", orderId, delayMinutes);
            rabbitMQUtil.sendDelayMessage(
                RabbitMqQueueEnum.DANGDANG_ORDER.getQueueName(), 
                orderId, 
                delayMinutes, 
                TimeUnit.MINUTES
            );
            log.info("铛铛订单处理延时消息发送成功，订单ID: {}, 延时: {}分钟", orderId, delayMinutes);
        } catch (Exception e) {
            log.error("发送铛铛订单处理延时消息异常，订单ID: {}, 延时: {}分钟, 错误信息: {}", 
                    orderId, delayMinutes, e.getMessage(), e);
            throw new RuntimeException("发送铛铛订单处理延时消息失败", e);
        }
    }

    /**
     * 发送带类型的铛铛订单处理消息（立即发送）
     * 
     * @param orderId 订单ID
     * @param messageType 消息类型
     */
    public void sendDangDangOrderMessage(Long orderId, String messageType) {
        try {
            if (orderId == null || orderId <= 0) {
                log.error("订单ID无效，无法发送铛铛订单消息: orderId={}", orderId);
                return;
            }

            log.info("发送铛铛订单处理消息，订单ID: {}, 类型: {}", orderId, messageType);
            rabbitMQUtil.sendMessage(
                RabbitMqQueueEnum.DANGDANG_ORDER.getQueueName(), 
                messageType, 
                orderId
            );
            log.info("铛铛订单处理消息发送成功，订单ID: {}, 类型: {}", orderId, messageType);
        } catch (Exception e) {
            log.error("发送铛铛订单处理消息异常，订单ID: {}, 类型: {}, 错误信息: {}", 
                    orderId, messageType, e.getMessage(), e);
            throw new RuntimeException("发送铛铛订单处理消息失败", e);
        }
    }

    /**
     * 发送带类型的铛铛订单处理消息（延时发送）
     * 
     * @param orderId 订单ID
     * @param messageType 消息类型
     * @param delay 延时时间
     * @param timeUnit 时间单位
     */
    public void sendDangDangOrderMessage(Long orderId, String messageType, long delay, TimeUnit timeUnit) {
        try {
            if (orderId == null || orderId <= 0) {
                log.error("订单ID无效，无法发送铛铛订单延时消息: orderId={}", orderId);
                return;
            }

            log.info("发送铛铛订单处理延时消息，订单ID: {}, 类型: {}, 延时: {}{}", 
                    orderId, messageType, delay, timeUnit.toString().toLowerCase());
            rabbitMQUtil.sendDelayMessage(
                RabbitMqQueueEnum.DANGDANG_ORDER.getQueueName(), 
                messageType, 
                orderId, 
                delay, 
                timeUnit
            );
            log.info("铛铛订单处理延时消息发送成功，订单ID: {}, 类型: {}, 延时: {}{}", 
                    orderId, messageType, delay, timeUnit.toString().toLowerCase());
        } catch (Exception e) {
            log.error("发送铛铛订单处理延时消息异常，订单ID: {}, 类型: {}, 延时: {}{}, 错误信息: {}", 
                    orderId, messageType, delay, timeUnit.toString().toLowerCase(), e.getMessage(), e);
            throw new RuntimeException("发送铛铛订单处理延时消息失败", e);
        }
    }

    /**
     * 发送铛铛订单重试消息
     * 
     * @param orderId 订单ID
     * @param retryCount 重试次数
     */
    public void sendDangDangOrderRetryMessage(Long orderId, int retryCount) {
        try {
            if (orderId == null || orderId <= 0) {
                log.error("订单ID无效，无法发送铛铛订单重试消息: orderId={}", orderId);
                return;
            }

            log.info("发送铛铛订单重试消息，订单ID: {}, 重试次数: {}", orderId, retryCount);
            
            String messageType = "DANGDANG_ORDER_RETRY_" + retryCount;
            // 重试延时递增：第1次延时30秒，第2次延时60秒，第3次延时120秒
            int delaySeconds = 30 * retryCount;
            rabbitMQUtil.sendDelayMessage(
                RabbitMqQueueEnum.DANGDANG_ORDER.getQueueName(), 
                messageType, 
                orderId, 
                delaySeconds, 
                TimeUnit.SECONDS
            );
            log.info("铛铛订单重试消息发送成功，订单ID: {}, 重试次数: {}, 延时: {}秒", 
                    orderId, retryCount, delaySeconds);
        } catch (Exception e) {
            log.error("发送铛铛订单重试消息异常，订单ID: {}, 重试次数: {}, 错误信息: {}", 
                    orderId, retryCount, e.getMessage(), e);
            throw new RuntimeException("发送铛铛订单重试消息失败", e);
        }
    }
}
